//在 node.js下获取 获取路径
const path = require('path')
//引用html-webpack-plugin 生成新的HTML模板
const HtmlWebpackPlugin = require('html-webpack-plugin');
//引用extract-text-webpack-plugin 把 CSS 分离成文件
const ExtractTextPlugin = require('extract-text-webpack-plugin');
//引用clean-webpack-plugin 打包前先清除dist文件夹
const CleanWebpackPlugin = require('clean-webpack-plugin');
//引用本地 webpack 功能
const webpack = require('webpack');



module.exports = {
  //定义输出
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[chunkhash].js'
  },
  //定义插件
  plugins: [
    //打包jQuery
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    //清空打包文件夹
    new CleanWebpackPlugin(['dist']),
    //重构首页模板
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
      minify: false,
      hash: process.env.NODE_ENV === 'production',
      excludeChunks: ['contact']
    }),
    //重构contact.html
    // new HtmlWebpackPlugin({
    //   template: './src/contact.html',
    //   filename: 'contact.html',
    //   minify: false,
    //   hash: process.env.NODE_ENV === 'production',
    //   chunks: ['contact']
    // }),
    //重构CSS文件
    new ExtractTextPlugin({
      filename: '[name].[contenthash].css',
      disable: false,
    }),
  ],
  //配置加载模块
  module: {
    rules: [
      //配置 sass 加载器
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader?sourceMap', 'sass-loader?sourceMap']
        })
      },
      //配置ES6加载器
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      //配置JSX加载器
      {
        test: /\.jsx$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      //配置jade(pug)加载器
      {
        test: /\.pug$/,
        loader: ['raw-loader', 'pug-html-loader']
      },
      //处理图片
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        use: [{
            loader: 'file-loader',
            options: {
              name: '[name].[ext]',
              outputPath: 'images/'
            }
          },
          {
            loader: 'image-webpack-loader',
            options: {
              bypassOnDebug: true,
            }
          }
        ]
      },
      //处理HTML中img标签
      {
        test: /\.html$/,
        use: [{
          loader: 'html-loader',
        }]
      },
      //处理字体
      {
        test: /\.woff2?$/,
        loader: 'url-loader?limit=10000&name=[name].[ext]&outputPath=fonts/'
      },
      {
        test: /\.(ttf|eot)$/,
        loader: 'file-loader?name=[name].[ext]&outputPath=fonts/'
      },
      {
        test: /bootstrap-sass[\/\\]assets[\/\\]javascripts[\/\\]/,
        loader: 'imports-loader?jQuery=jquery'
      },

    ]
  }

}